library PyMinMod;
 
{$LINKLIB c}
{$LINKLIB dl}

uses
  interfaces,
  SysUtils,
  PythonEngine;
 
 
function SumTwoIntegers(Self, Args: PPyObject): PPyObject; cdecl;
var
  Arg1 : Integer;
  Arg2 : Integer;
begin
  with GetPythonEngine do begin
    PyArg_ParseTuple(Args, 'ii', @Arg1, @Arg2);
    Result := PyInt_FromLong(Arg1 + Arg2);
  end;
end;
 
 
function ConcatTwoStrings(Self, Args: PPyObject) : PPyObject; cdecl;
 {From Python documentation for "s" format: "You must not provide storage for 
   the string itself; a pointer to an existing string is stored into the 
   character pointer variable whose address you pass."
  From Python documentation for PyString_FromString: "Return a new string 
   object with a copy of the string v as value on success".}
var
  Arg1 : PAnsiChar;
  Arg2 : PAnsiChar;
begin
  with GetPythonEngine do begin
    PyArg_ParseTuple(Args, 'ss', @Arg1, @Arg2);  //Get the two string arguments
    Result := PyString_FromString(PAnsiChar(AnsiString(Arg1) + AnsiString(Arg2)));  //Concatenate and return string
  end;
end;
 
 
var
  Methods : packed array [0..2] of PyMethodDef;

 
var
  gEngine : TPythonEngine;
  gModule : TPythonModule;

function PyInit_PyMinMod: PPyObject;
begin
  Result := nil;
  //try
    gEngine := TPythonEngine.Create(nil);
    gEngine.AutoFinalize := False;
    gEngine.UseLastKnownVersion := False;
    gEngine.RegVersion := '3.5';
    gEngine.APIVersion := 1013;
    gEngine.DllName := 'libpython3.5m.so';
    gEngine.LoadDll;

    gModule := TPythonModule.Create(nil);
    gModule.Engine := gEngine;
    gModule.ModuleName := 'PyMinMod';
    gModule.AddMethod( 'sum_two_integers', @SumTwoIntegers, 'add(a,b) -> a+b' );
    gModule.Initialize;
    Result := gModule.Module;
  //except
  //end;
end;

   
exports
  PyInit_PyMinMod;
 
end.
